package com.ibm.ems.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ibm.ems.dto.UserBean;

public class ParticipantsDAO {
	Connection conn;
	public ParticipantsDAO() {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle","hr","hr");
		} catch (SQLException e) {
			
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}
	}
	public void getParticipantdetails(UserBean pr){
			
		try {
				String role_name="Participant";
				
				Statement stmt1=conn.createStatement();
				Statement stmt2=conn.createStatement();
				Statement stmt3=conn.createStatement();
				
				String query1="insert into users(user_name,user_pass) values('"+pr.getUserName()+"','"+pr.getUserPass()+"')";
				String query2="insert into user_roles(user_name,role_name) values('"+pr.getUserName()+"','"+role_name+"')";
				String query3="insert into user_profile(user_name,user_fullname,user_phone,user_email,user_add) values('"+pr.getUserName()+"','"+pr.getFullName()+"',"+pr.getUserNum()+",'"+pr.getUserEmail()+"','"+pr.getUserAddress()+"')";
				
				stmt1.executeUpdate(query1);
				stmt2.executeUpdate(query2);
				stmt3.executeUpdate(query3);
				
				System.out.println("success");
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
			
	}
		
	public int checkUsername(UserBean pr){
		int flagvariable=1;
		try
		{
		String namefromForm=pr.getUserName();
		
		String query="select * from users";
		Statement statement=conn.createStatement();
		ResultSet rs=statement.executeQuery(query);
			
			if(rs!=null){
				while(rs.next())
				{
					String fromDatabase=rs.getString(1);
					if(namefromForm.equals(fromDatabase))
						flagvariable=0;
				}
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return flagvariable;
	}
		
		
	}


